package com.tongtong.stsuserapi.service.base.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tongtong.stscommon.enumration.CollectionCodeState;
import com.tongtong.stscommon.enumration.Message;
import com.tongtong.stscommon.enumration.Status;
import com.tongtong.stscommon.exception.STSResourcesOperationException;
import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.message.STSSuccessMessage;
import com.tongtong.stscommon.utils.checker.ObjectChecker;
import com.tongtong.stsuserapi.dao.ClassificationDao;
import com.tongtong.stsuserapi.pojo.Classification;
import com.tongtong.stsuserapi.pojo.CollectionCode;
import com.tongtong.stsuserapi.service.base.ClassificationService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ClassificationServiceImpl implements ClassificationService {

	@Autowired
	ClassificationDao classificationDao;

	@Override
	public STSMessage getClassificationList(Integer page, Integer pageSize) {
		Page<Classification> pageInfo = new Page(page, pageSize);
		Page<Classification> codePage = classificationDao.selectPage(pageInfo, null);
		List<Classification> list = codePage.getRecords();
		Long len = classificationDao.selectCount(null);

		HashMap<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("len",len);

		return STSSuccessMessage.from(map);
	}

	@Override
	public STSMessage addClassification(Long managerId, String classificationName) {

		Classification classification = new Classification();

		classification.setClassificationName(classificationName);
		classification.setManagerId(managerId);

		try	{
			classificationDao.insert(classification);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.AddResourcesError, e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK,Message.OK);
	}

	@Override
	public STSMessage alterClassification(Long managerId, Long classificationId, String classificationName) {

		Classification classification = classificationDao.selectById(classificationId);
		ObjectChecker.ObjectsFromDBNullCheck(classification);

		classification.setClassificationName(classificationName);
		classification.setManagerId(managerId);

		try	{
			classificationDao.updateById(classification);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.UpdateResourcesError,e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK,Message.OK);
	}

	@Override
	public STSMessage deleteClassificationByList(List<Long> classificationIds) {

		try {
			classificationDao.deleteBatchIds(classificationIds);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.DeleteResourcesError,e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK,Message.OK);
	}
}
